<!-- 绑定权限组 -->
<template>
  <div class="bind-permission-content">
    <div class="bind-permission-content-main">
      <h3>权限组</h3>
      <div class="tree-main">
        <Tree
          :data="listData"
          ref="TreeNode"
          show-checkbox
          multiple
          :load-data="loadData"
          @on-check-change="checkChange"
        ></Tree>
      </div>
      <div class="submit-footer">
        <Button @click="cencal()">关闭</Button>
        <Button type="primary" :loading="isLoading" @click="handleSubmit"
          >确定</Button
        >
      </div>
    </div>
  </div>
</template>

<script type="text/javascript">
import {
  getGoupList, // 获取权限组
  authorityList, // 获取权限组下面权限
  bindRoleAuth, // 绑定角色权限
  getBindAuthIdDetail // 获取绑定的权限
} from "@/api/system.js";
import util from "@/libs/util";

import iconList from "@/libs/icon-list";
const companyId = util.cookies.get("companyId");

export default {
  name: "bindPermission",
  components: {},
  props: {
    permissionRoleId: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      formData: {
        permissionGroupId: "",
        companyId,
        groupName: "",
        orderNo: 0,
        groupType: "Auth",
      },
      ruleValidate: {},
      modalValue: false,
      isLoading: false,
      iconList,
      listData: [],
      permissionAuthorityIdList:[], // 当前用户绑定的权限
    };
  },
  methods: {
    cancel() {
      this.$emit("update:updateIsHide", false);
    },
    async createGroupAction() {
      this.isLoading = true;
      try {
        let data = await createGroup(this.formData);
        this.isLoading = false;
        this.$Message.success("添加成功！");
        this.$emit("update:updateIsHide", false);
        this.$emit("actionSuccess");
      } catch (error) {
        this.isLoading = false;
      }
    },
    async updateGroupAction() {
      this.isLoading = true;
      try {
        let data = await updateGroup(this.formData);
        this.isLoading = false;
        this.$Message.success("编辑成功！");
        this.$emit("update:updateIsHide", false);
        this.$emit("actionSuccess");
      } catch (error) {
        this.isLoading = false;
      }
    },
    handleSubmit() {
      this.getSelectNode();
    },
    async getGoupListData() {
      let params = {
        groupName: "",
        groupType: "Auth",
      };
      let data = await getGoupList(params);
      data.forEach((o) => {
        o.title = o.groupName;
        o.loading = false;
        o.children = [];
      });
      this.listData = data;
    },
    async loadData(item, callback) {
      console.log(item, "item");
      let params = {
        companyId: item.companyId,
        permissionGroupId: item.permissionGroupId,
      };
      let data = await authorityList(params);
      data.forEach((o) => {
        o.title = o.authName;
        o.id = o.permissionAuthorityId;
        if(this.permissionAuthorityIdList.includes(o.permissionAuthorityId)){
          o.checked = true
        }
      });
      callback(data);
    },
    checkChange() {},
    async getSelectNode() {
      let checkNode = this.$refs.TreeNode.getCheckedNodes();
      let authList = checkNode
        .filter(o => o.nodeKey == 1)
        .map((o) => o.permissionAuthorityId);
      let params = {
        permissionRoleId: this.$props.permissionRoleId,
        permissionAuthorityIdList: authList,
      };
      try {
        if (authList.length == 0) {
          return this.$Message.error("请选择需要绑定的权限！");
        }
        this.isLoading = true;
        let data = await bindRoleAuth(params);
        this.$Message.success("权限绑定成功！");
        this.$emit("update:updateIsHide", false);
        this.isLoading = false;
      } catch (error) {
        this.isLoading = false;
      }
      console.log(this.$refs.TreeNode.getCheckedNodes(), authList);
    },
    async getBindAuthIdDetailData(){
      let params = {
        permissionRoleId:this.$props.permissionRoleId
      }
      let data = await getBindAuthIdDetail(params)
      console.log(data,'data')
      this.permissionAuthorityIdList = data.permissionAuthorityIdList
      this.getGoupListData();
    },
    cencal() {
      this.$emit("update:updateIsHide", false);
    },
  },
  created() {
    this.getBindAuthIdDetailData()
  },
};
</script>

<style lang="less" scoped>
.ivu-radio-group {
  width: 100%;
}
.ivu-input-number {
  width: 100%;
}
.bind-permission-content {
  position: relative;
}
.bind-permission-content-main {
  padding-bottom: 60px;
  height: calc(~"100vh - 84px");
  box-sizing: border-box;
  overflow: auto;
  .submit-footer {
    position: absolute;
    padding: 10px;
    display: flex;
    bottom: 0;
    justify-content: center;
    background: #fff;
    width: 100%;
    .ivu-btn {
      margin-left: 10px;
    }
    .ivu-btn-default {
      color: #0052d9;
      background: rgba(45, 143, 255, 0.14);
      border-color: rgba(45, 143, 255, 0.14);
    }
  }
}
.tree-main {
  padding-bottom: 80px;
}
</style>
